package itxinhaimijijia.xjfjiawen.controller;//package itxinhaimijijia.xjfjiawen.controller;


import cn.hutool.core.io.IoUtil;
import itxinhaimijijia.xjfjiawen.Pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {

    @RequestMapping("/list")
    public  List<User> list() throws Exception{

        //读取user.text文件，解析文件中的内容并保存在user集合中
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("user.txt");
        // 添加空值检查，确保文件存在
        if (resourceAsStream == null) {
            throw new RuntimeException("无法找到 user.text 文件");
        }
//        System.out.println("resourceAsStream============="+resourceAsStream);
        ArrayList<String> lines = IoUtil.readLines(resourceAsStream, StandardCharsets.UTF_8, new ArrayList<>());



        //处理和解析保存在文件中的内容
//        lines.map(itrem)
        List<User> userlist = lines.stream().map(item -> {
            String[] parts = item.split(",");
            Integer SerialNumber = Integer.parseInt(parts[0]);
            String username = parts[1];
            String password = parts[2];
            String nickname = parts[3];
            Integer age = Integer.parseInt(parts[4]);
            LocalDateTime updateTime = LocalDateTime.parse(parts[5],
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//            LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new User(SerialNumber, username, password, nickname, age, updateTime);
        }).collect(Collectors.toList());

        return userlist;


    }

}
